﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TTBusiness.DB_Linq;
using TTBusiness.ClassDB;
using TTBusiness;
namespace TTController
{
    public class clsCTProcess
    {
        TTMarketing_DBDataContext objDB = new TTMarketing_DBDataContext();
        public List<tblProduct> Product_getTop(int top, bool status, bool visible)
        {
            try
            {
                var l = from p in objDB.tblProducts
                        where p.Status.Equals(status) && p.Visible.Equals(visible)
                        select p;
                return l.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<tblProduct> Product_getTopHOT(int statusID,int top, bool status, bool visible)
        {
            try
            {
                var l = from s in objDB.tblStatus_Products
                        where s.StatusID.Equals(statusID) &&
                        s.tblProduct.Status.Equals(status) && s.tblProduct.Visible.Equals(visible)
                        select s.tblProduct;
                return l.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblProduct> Product_getTopByStatus(int top, bool status, bool visible)
        {
            try
            {
                var l = from p in objDB.tblProducts
                        where p.Status.Equals(status) && p.Visible.Equals(visible)
                        select p;
                return l.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        
        public List<tblProduct> Product_getByFactory(int factoryid, bool status, bool visible)
        {
            try
            {
                var l = from p in objDB.tblProducts
                        where p.Status.Equals(status) && p.Visible.Equals(visible)
                        && p.tblCate_Factory.CateID.Equals(factoryid)
                        select p;
                return l.ToList<tblProduct>();
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        public List<tblProduct> Product_getByCate(int cateid, bool status, bool visible)
        {
            try
            {
                var l = from p in objDB.tblProducts
                        where p.Status.Equals(status) && p.Visible.Equals(visible)
                        && p.tblCate_Factory.FactoryID.Equals(cateid)
                        select p;
                return l.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblProduct> Product_getAll(bool status, bool visible)
        {
            try
            {
                var l = from p in objDB.tblProducts
                        where p.Status.Equals(status) && p.Visible.Equals(visible)
                        select p;
                return l.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblProduct> Product_getInID(bool status, bool visible, List<int> listID)
        {
            //List id dang : int[] listID = {10,11,12};
            int[] list = listID.ToArray();
            try
            {
                var l = from p in objDB.tblProducts
                        where p.Status.Equals(status) && p.Visible.Equals(visible) && list.Contains(p.ID)
                        select p;
                return l.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblProduct> Product_getByFactoryAndCategory(int categoryid,int factoryid, bool status, bool visible)
        {
            try
            {
                var l = from p in objDB.tblProducts
                        where p.Status.Equals(status) && p.Visible.Equals(visible)
                        && p.tblCate_Factory.FactoryID.Equals(factoryid)
                        && p.tblCate_Factory.CateID.Equals(categoryid)
                        select p;
                return l.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<tblManuFactory> getFactoryByCateNotInNoCate(int categoryid,int idNoCate)
        {
            try
            {
                var list = from a in objDB.tblCate_Factories
                           where a.CateID.Equals(categoryid)
                           && !a.FactoryID.Equals(idNoCate)
                           select a.tblManuFactory;
                return list.ToList<tblManuFactory>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public int Order_insert(tblOrder obj)
        {
            try
            {
                obj.Date = DateTime.Now;
                objDB.tblOrders.InsertOnSubmit(obj);
                objDB.SubmitChanges();
                var i = (from o in objDB.tblOrders
                         select o.ID).Max();
                return i;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblNew> News_getByTopHot(int top, bool status, bool hot)
        {
            try
            {
                var list = (from n in objDB.tblNews
                            where n.Status.Equals(status) && n.Hot.Equals(hot)
                            select n).Take<tblNew>(top);
                return list.ToList<tblNew>();
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        public List<clsBSGetOrder> Order_getOrder(List<clsBSOrder> orderlist)
        {
            try
            {
                List<clsBSGetOrder> l = new List<clsBSGetOrder>();
                foreach (var item in orderlist)
                {
                    clsBSGetOrder obj = new clsBSGetOrder();
                    clsBSProduct objB = new clsBSProduct();
                    obj.Product = objB.getById(item.ProductId);
                    obj.Count = item.Count;
                    l.Add(obj);
                }
                return l;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblSupport> Support_GetAll()
        {
            try
            {
                var l = from s in objDB.tblSupports
                        select s;
                List<tblSupport> list = new List<tblSupport>();
                list = l.ToList<tblSupport>();
                foreach (var item in list)
                {
                    item.Nick = item.tblTypeSupport.Pattern.Replace(item.tblTypeSupport.Parameter, item.Nick);
                }
                return list;
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        
    }
}
